package org.fh;

import org.fh.inductionLamp.StatusHandle;
import org.fh.nettyService.NettyServer;
import org.fh.nettyService.NettyServerHandler;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;

import java.net.InetSocketAddress;

/**
 * @Author: wwy
 * @Date: 2020/8/28 11:16
 */
@SpringBootApplication
public class ServerApplication {
    public static SendData sendData;

    public static void main(String[] args) {
        ApplicationContext applicationContext = SpringApplication.run(ServerApplication.class, args);
        NettyServerHandler.applicationContext = applicationContext;
        StatusHandle.applicationContext = applicationContext;
        SendData.applicationContext = applicationContext;
        String ip = applicationContext.getEnvironment().getProperty("netty.ip");
        String port = applicationContext.getEnvironment().getProperty("netty.port");
        sendData = new SendData();

        Thread thread = new Thread(()->{
            //启动服务端
            NettyServer nettyServer = new NettyServer();
            nettyServer.start(new InetSocketAddress(Integer.parseInt(port)));
        });
        thread.start();

    }

    @Bean //必须new 一个RestTemplate并放入spring容器当中,否则启动时报错
    public RestTemplate restTemplate() {
        HttpComponentsClientHttpRequestFactory httpRequestFactory = new HttpComponentsClientHttpRequestFactory();
        httpRequestFactory.setConnectionRequestTimeout(30 * 1000);
        httpRequestFactory.setConnectTimeout(30 * 3000);
        httpRequestFactory.setReadTimeout(30 * 3000);
        return new RestTemplate(httpRequestFactory);
    }
}
